﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Ommf
{
    public class Animation : IDrawable
    {
        private Image image;
        private int frameWidth;
        private int frameHeight;
        private int numFrames;
        private double delay;

        private int currentFrame = 0;
        private double deltaSum = 0;
        private AnimationStyle style = AnimationStyle.Loop;
        private bool isPlaying = true;
        private bool inReverse = false;

        internal Animation(Image image, int frameWidth, int frameHeight, int numFrames, double delay)
        {
            this.image = image;
            this.frameWidth = frameWidth;
            this.frameHeight = frameHeight;
            this.numFrames = numFrames;
            this.delay = delay;
        }

        public void Update(double delta)
        {
            if (!isPlaying) return;

            deltaSum += delta;
            if (deltaSum >= delay)
            {
                deltaSum = 0;

                switch (style)
                {
                    case AnimationStyle.Once:
                        currentFrame++;
                        if (currentFrame >= numFrames)
                        {
                            isPlaying = false;
                            currentFrame = numFrames - 1;
                        }
                        break;

                    case AnimationStyle.Loop:
                        currentFrame = (currentFrame + 1) % numFrames;
                        break;

                    case AnimationStyle.Bounce:
                        if (inReverse)
                        {
                            currentFrame--;
                            if (currentFrame <= 0)
                            {
                                inReverse = false;
                                currentFrame = 0;
                            }
                        }
                        else
                        {
                            currentFrame++;
                            if (currentFrame >= numFrames - 1)
                            {
                                inReverse = true;
                                currentFrame = numFrames - 1;
                            }
                        }
                        break;
                }
            }
        }

        public void Pause()
        {
            isPlaying = false;
        }

        public void Resume()
        {
            isPlaying = true;
        }

        public void Reset()
        {
            isPlaying = true;
            currentFrame = 0;
            inReverse = false;
            deltaSum = 0;
        }

        #region IDrawable Members

        public void Draw(float x, float y, float angle, float scaleX, float scaleY)
        {
            image.DrawSubrect(x, y, angle, scaleX, scaleY, currentFrame * frameWidth, 0, frameWidth, frameHeight);
        }

        public void DrawSubrect(float x, float y, float angle, float scaleX, float scaleY, float subX, float subY, float subWidth, float subHeight)
        {
        }

        #endregion

        public bool IsPlaying
        {
            get { return isPlaying; }
        }

        public int CurrentFrame
        {
            get { return currentFrame; }
            set { currentFrame = value % numFrames; }
        }

        public double Delay
        {
            get { return delay; }
            set { delay = value; }
        }

        public AnimationStyle Style
        {
            get { return style; }
            set { style = value; }
        }
    }
}
